package com.come4loves.game.dataconfig.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.come4loves.core.dataconfig.ServiceAdapter;
import com.come4loves.core.utils.RandomUtils;
import com.come4loves.game.dataconfig.model.RandomShopConfig;
import com.come4loves.game.module.card.constant.Quality;

/**
 * 將軍配置服务
 * 
 *
 */
@Component
public class ShopService extends ServiceAdapter {

	/**
	 * card
	 */
	private static Map<Integer, RandomShopConfig> cardConfigMap = new HashMap<Integer, RandomShopConfig>();
	/**
	 * areaType, List<RandomShopConfig>
	 */
	private static Map<Integer, List<RandomShopConfig>> areaTypeMap = new HashMap<Integer, List<RandomShopConfig>>();
	/**
	 * Quality,List<RandomShopConfig>
	 */
	private static Map<Quality, List<RandomShopConfig>> qualityMap = new HashMap<Quality, List<RandomShopConfig>>();

	@Override
	public void clear() {
		cardConfigMap.clear();
	}

	@Override
	public void initialize() {
		List<RandomShopConfig> shopConfigs = this.dataConfig.listAll(this, RandomShopConfig.class);
		for (RandomShopConfig config : shopConfigs) {
			cardConfigMap.put(config.getId(), config);
			Quality quality = config.getQuality();
			if (qualityMap.containsKey(quality) == false) {
				qualityMap.put(quality, new ArrayList<RandomShopConfig>());
			}
			qualityMap.get(quality).add(config);
			int areaType = config.getArenaType();
			if (areaTypeMap.containsKey(areaType) == false) {
				areaTypeMap.put(areaType, new ArrayList<RandomShopConfig>());
			}
			areaTypeMap.get(areaType).add(config);
		}
		cardConfigMap = Collections.unmodifiableMap(cardConfigMap);
		areaTypeMap = Collections.unmodifiableMap(areaTypeMap);
		qualityMap = Collections.unmodifiableMap(qualityMap);
	}

	
	public static RandomShopConfig getShopConfig(int id) {
		return cardConfigMap.get(id);
	}
	
	public static boolean exsitShopConfig(int goodsId) {
		return cardConfigMap.containsKey(goodsId);
	}
	
	
	public static int getBoxAllWeight(int arenaType) {
		int total = 0;
		for (RandomShopConfig boxConfig : cardConfigMap.values()) {
			if (arenaType == boxConfig.getArenaType()) {
				total += boxConfig.getWeightBox();
			}
		}
		return total;
	}

	public static int getCardAllWeight(int arenaType) {
		int total = 0;
		for (RandomShopConfig shopConfig : cardConfigMap.values()) {
			if (arenaType == shopConfig.getArenaType()) {
				total += shopConfig.getWeightCard();
			}
		}
		
//		RandomUtils.randomHit(total, map)
		return total;
	}
//	
//	public static Map<Integer, RandomShopConfig> getCardConfigMapByArenaType(int arenaType) {
//		Map<Integer, RandomShopConfig> collection = new LinkedHashMap<Integer, RandomShopConfig>();
//		for (RandomShopConfig shopConfig : cardConfigMap.values()) {
//			if (arenaType == shopConfig.getArenaType()) {
//				collection.put(shopConfig.getId(), shopConfig);
//			}
//		}
//		return collection;
//	}
	
	public static Collection<RandomShopConfig> randomConfigByArena(int size, int arenaType, boolean isBox) {
		Map<Integer, RandomShopConfig> configs = new HashMap<Integer, RandomShopConfig>();
		if (areaTypeMap.containsKey(arenaType) == false || areaTypeMap.get(arenaType).size() < size) {
			return Collections.emptyList();
		} else {
			List<RandomShopConfig> list = areaTypeMap.get(arenaType);
			Map<Integer, Integer> rateMap = new HashMap<Integer, Integer>();
			int totalWeight = 0;
			for (RandomShopConfig randomShopConfig : list) {
				int configWeight = randomShopConfig.getWeight(isBox);
				rateMap.put(randomShopConfig.getId(), configWeight);
				totalWeight += configWeight;
			}
			for (int i = 0; i < size; i++) {
				Integer randomHitId = RandomUtils.randomHit(totalWeight, rateMap);
				while (configs.containsKey(randomHitId)) {
					randomHitId = RandomUtils.randomHit(totalWeight, rateMap);
				}
				configs.put(randomHitId, cardConfigMap.get(randomHitId));
			}
		}
		return configs.values();
	}
	
}
